package kr.hkit.androidcomplete02.alarm;

import java.util.Locale;

import kr.hkit.androidcomplete02.R;
import android.app.Activity;
import android.app.TimePickerDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TimePicker;

public class TTS extends Activity implements OnInitListener {
	TextToSpeech _tts;
	boolean _ttsActive = false;
	EditText editT;
	Button bt_start;
	String str;
	CheckBox chk1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tts_main);
		editT = (EditText) findViewById(R.id.edittts);
		
		SharedPreferences pref = getSharedPreferences("PrefTts", 0);
		String target = pref.getString("target", "나의 목표 또는 할 일을 입력하세요");
		editT.setText(target);
		
		bt_start = (Button) findViewById(R.id.btntts);
		bt_start.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				str = editT.getText().toString();
				_tts.setLanguage(Locale.KOREA);
				_ttsActive = true;
				_tts.speak(str, TextToSpeech.QUEUE_FLUSH, null);
			}
		});
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		_tts = new TextToSpeech(getApplicationContext(), this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		try{
			if(_tts != null){
				_tts.stop();
				_ttsActive = false;
			}
			SharedPreferences pref = getSharedPreferences("PrefTts", 0);
			SharedPreferences.Editor edit = pref.edit();
			
			String target = editT.getText().toString();
			
			edit.putString("target", target);
			
			edit.commit();
		}catch(Exception e){
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		try{
			if(_tts != null){
				_tts.shutdown();
				_tts = null;
			}
		}catch(Exception e){
		}
	}

	@Override
	public void onInit(int status) {
	}
	
	/*private void DialogTimePicker(){
		chk1 = (CheckBox) findViewById(R.id.chkbox1);
	    TimePickerDialog.OnTimeSetListener mTimeSetListener = 
	    new TimePickerDialog.OnTimeSetListener() {
			@Override
			public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
				String time = hourOfDay + " : " + minute;
				chk1.setText(time);
			}
	    };
	    TimePickerDialog alert = new TimePickerDialog(this,	mTimeSetListener, 0, 0, false);
	    alert.show();
	}//타임피커 다이얼 로그창*/
}
